package com.sunshine.lnuplus.base

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.Toolbar
import com.sunshine.lnuplus.R
import kotlinx.android.synthetic.main.activity_base.*
import kotlinx.android.synthetic.main.activity_base.view.*

open class BaseActivity : AppCompatActivity(){

    var color = 0
    var darkColor = 0
    var isNight = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isNight = getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)
        if(isNight){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            //setTheme(R.style.grayTheme)
            color = resources.getColor(R.color.night)
        }else{
            setTheme()
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        }
    }

    override fun setContentView(layoutResID: Int) {
        val view = LayoutInflater.from(this).inflate(layoutResID, null, false)
        val baseView = LayoutInflater.from(this).inflate(R.layout.activity_base, null, false)
        view.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) as ViewGroup.LayoutParams?
        baseView.base_container.addView(view)
        window.setContentView(baseView)
        initToolBar(baseView.base_toolbar)
    }

    private fun initToolBar(toolbar: Toolbar){
        setSupportActionBar(toolbar)
        val actionBar: ActionBar? = supportActionBar
        if(actionBar != null){
            actionBar.setDisplayShowTitleEnabled(false)
            actionBar.setDisplayHomeAsUpEnabled(true)
        }
        toolbar.setTitleTextColor(resources.getColor(R.color.white))
        toolbar.setNavigationOnClickListener{
            supportFinishAfterTransition()
        }
    }

    protected fun getToolBar(): Toolbar? {
        return base_toolbar
    }

    override fun setTitle(title: CharSequence?) {
        base_toolbar.title = title
    }

    fun setNoTitle(){
        base_toolbar.visibility = View.GONE
    }

    fun setShowTitle(){
        base_toolbar.visibility = View.VISIBLE
    }

    private fun setTheme(){
        when (getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getString("theme", "5")?.toInt()) {
            1 -> { setTheme(R.style.blackTheme)
                color = resources.getColor(R.color.primary_black)
                darkColor = resources.getColor(R.color.primary_dark_black)}
            2 -> {setTheme(R.style.purpleTheme)
                color = resources.getColor(R.color.primary_purple)
                darkColor = resources.getColor(R.color.primary_dark_purple)}
            3 -> {setTheme(R.style.purple_deTheme)
                color = resources.getColor(R.color.primary_purple_de)
                darkColor = resources.getColor(R.color.primary_dark_purple_de)}
            4 -> {setTheme(R.style.pinkTheme)
                color = resources.getColor(R.color.primary_pink)
                darkColor = resources.getColor(R.color.primary_dark_pink)}
            5 -> {setTheme(R.style.yellowTheme)
                color = resources.getColor(R.color.primary_yellow)
                darkColor = resources.getColor(R.color.primary_dark_yellow)}
            6 -> {setTheme(R.style.blueTheme)
                color = resources.getColor(R.color.primary_blue)
                darkColor = resources.getColor(R.color.primary_dark_blue)}
            7 -> {setTheme(R.style.greenTheme)
                color = resources.getColor(R.color.primary_green)
                darkColor = resources.getColor(R.color.primary_dark_green)}
        }
    }

    fun getTimetableTheme(): Int? {
        if(isNight) return 1
        return getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE)?.getInt("timetable_theme", 3)
    }
}